package com.hs.ph.Servlet;

import com.alibaba.fastjson.JSON;
import com.hs.ph.Dao.CommunityDao;
import com.hs.ph.Dao.ShopDao;
import com.hs.ph.Dao.impl.CommunityDaoImpl;
import com.hs.ph.Dao.impl.ShopDaoImpl;
import com.hs.ph.Entity.Community;
import com.hs.ph.Entity.Result;
import com.hs.ph.Entity.Shop;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/community")
public class CommunityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //搜索请求-----------------------------------------------
        CommunityDao communityDao = new CommunityDaoImpl();
        String action = req.getParameter("action");
        if(action.equals("selectAll")){
            Map<String,String> map = new HashMap<>();

            String communityName = req.getParameter("communityName");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            map.put("communityName",communityName);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            List<Community> communities = communityDao.selectAll(map);
            int total = communityDao.selectCount(map);
            Map map1 = new HashMap<>();
            map1.put("total",total);
            map1.put("list",communities);
            Result result = Result.success(map1);
            resp.getWriter().write(JSON.toJSONString(result));
        } else if (action.equals("update")) {

            int addOrUpdateFlag = Integer.parseInt(req.getParameter("addOrUpdateFlag"));
            String communityAddress = req.getParameter("communityAddress");
            String Code = req.getParameter("communityAreaCode");
            int communityAreaCode=0;
            if (Code!=null && !"".equals(Code)){
                communityAreaCode= Integer.parseInt(Code);
            }
            String communityCity = req.getParameter("communityCity");
            String communityCoordinates = req.getParameter("communityCoordinates");
            String communityDistrict = req.getParameter("communityDistrict");
            String communityid = req.getParameter("communityId");
            int communityId=0;
            if (communityid!=null && !"".equals(communityid)) {
                communityId = Integer.parseInt(communityid);
            }
            String communityLatitude = req.getParameter("communityLatitude");
            String communityLongitude = req.getParameter("communityLongitude");
            String communityName = req.getParameter("communityName");
            String communityProvince = req.getParameter("communityProvince");
            Community community=new Community( communityName,  communityProvince,  communityCity,  communityDistrict,  communityAddress,  communityLatitude,  communityLongitude,
                    communityAreaCode, communityId, communityCoordinates);

            if (addOrUpdateFlag==1){
                int num = communityDao.insert(community);
                if (num!=0){
                    Result result = Result.success("添加成功");
                    resp.getWriter().write(JSON.toJSONString(result));
                }else {
                    Result result = Result.error("添加失败");
                    resp.getWriter().write(JSON.toJSONString(result));
                }
            }else {
                int num = communityDao.update(community);
                if (num!=0){
                    Result result = Result.success("修改成功");
                    resp.getWriter().write(JSON.toJSONString(result));
                }else {
                    Result result = Result.error("修改失败");
                    resp.getWriter().write(JSON.toJSONString(result));
                }
            }


        } else if (action.equals("delete")) {
            int communityId = Integer.parseInt(req.getParameter("communityId"));
            int num = communityDao.delete(communityId);
            if (num!=0){
                Result result = Result.success("删除成功");
                resp.getWriter().write(JSON.toJSONString(result));
            }else {
                Result result = Result.error("删除失败");
                resp.getWriter().write(JSON.toJSONString(result));
            }
        } else if (action.equals("associatedStoresList")) {
            Map<String,String> map = new HashMap<>();
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String shopName = req.getParameter("shopName");
            String communityId = req.getParameter("communityId");
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("shopName",shopName);
            map.put("communityId",communityId);
            List<Shop> shops = communityDao.selectGuanLian(map);
            int total = communityDao.selectGuanLianCount(map);

            Map map1 = new HashMap<>();
            map1.put("total",total);
            map1.put("list",shops);
            Result result = Result.success(map1);
            resp.getWriter().write(JSON.toJSONString(result));
        } else if (action.equals("findShopListByAdd")) {
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String parameter = req.getParameter("shopName: ");
            Map map = new HashMap<>();
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("shopName",parameter);
            ShopDao shopDao=new ShopDaoImpl();
            List list = shopDao.selectAll(map);
            int total = shopDao.selectCount(map);
            Map map1 = new HashMap<>();
            map1.put("total",total);
            map1.put("list",list);
            Result result = Result.success(map1);
            resp.getWriter().write(JSON.toJSONString(result));
        } else if (action.equals("addAssociatedStores")) {
            String[] fendeShopCommunityLists = req.getParameterValues("fendeShopCommunityList");
            String panghuCommunityid = req.getParameter("panghuCommunityId");
            int panghuCommunityId=0;
            if (panghuCommunityid!=null && !"".equals(panghuCommunityid)) {
                panghuCommunityId = Integer.parseInt(panghuCommunityid);
            }

            int num = communityDao.updateShopCommunityId(fendeShopCommunityLists, panghuCommunityId);
            if (num!=0){
                Result result = Result.success("增加成功");
                resp.getWriter().write(JSON.toJSONString(result));
            }else {
                Result result = Result.error("增加失败");
                resp.getWriter().write(JSON.toJSONString(result));
            }
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
